package com.financing.wap.web.controller.api;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.financing.wap.dto.UserTrackingDTO;
import com.financing.wap.service.IUserTrackingService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("userTrackingController")
@RequestMapping("/api/t")
public class UserTrackingController extends BaseController  { 
	

	/**
	 * 用户类型  01:活动
	 */
	public static final String USER_TYPE_ACTIVITIES = "01";
	
	/**
	 * 用户类型  02:广告
	 */
	public static final String USER_TYPE_ADVERT = "02";
	
	
	/**
	 * 默认 sendRedirect
	 */
	public static final String DEFAULT_SEND_REDIRECT = "http://www.superfa.cn";

	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private  IUserTrackingService  userTrackingServiceImpl;
	
	/**
	 * 交易详情
	 * @return
	 */
	@RequestMapping("/{userType}/{trackType}/{trackId}")
	public void view(@PathVariable String trackId,@PathVariable String trackType,@PathVariable String userType,HttpServletRequest request, HttpServletResponse response){ 
		UserTrackingDTO utdto = new UserTrackingDTO();
		utdto.setUserType(userType);
		utdto.setTrackId(trackId);
		utdto.setTrackType(trackType);
		utdto.setUpdateBy(trackId);
		utdto.setCreateBy(trackId);
		try{
			userTrackingServiceImpl.addUserTracking(utdto);
		}catch(Exception e){
			log.error("add user tracking error!",e);
		}finally{
			try {
				if(USER_TYPE_ACTIVITIES.equals(userType)){
					response.sendRedirect("http://wap.superfa.cn/#/hd/zcshb/register?_mid=-1");
					return ;
				}else if(USER_TYPE_ADVERT.equals(userType)){ 
					//用户类型 ：广告  是否有参数u
					String url = request.getParameter("u");
					if(!StringUtil.isNullOrEmptyStr(url)){
						response.sendRedirect(url);
						return ;
					}
					
				}
				
				response.sendRedirect(DEFAULT_SEND_REDIRECT);
				return ;
			} catch (IOException e) {
				log.error("send redirect error!",e);
			}
		}
		
	}

}
